package org.grizz.game.entities;

import java.util.Random;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;

public class LineEntity implements Entity {
	private ShapeRenderer renderer;
	private Rectangle area;
	
	private MovingPoint point1;
	private MovingPoint point2;
	
	private Color color;
	
	public LineEntity(ShapeRenderer renderer, Rectangle area) {
		this.renderer = renderer;
		this.area = area;
		
		float leftEndX = area.x + (area.width / 3);
		float leftEndY = area.y + (area.height / 3);
		
		float rightEndX = (area.x + area.width) - (area.width / 3);
		float rightEndY = (area.y + area.height) - (area.height / 3);
		
		Random rand = new Random();
		
		point1 = new MovingPoint()
			.setLocation(new Vector2(leftEndX, leftEndY))
			.setSpeed(new Vector2(rand.nextFloat() * 2 - 1, rand.nextFloat() * 2 - 1));
		
		point2 = new MovingPoint()
			.setLocation(new Vector2(rightEndX, rightEndY))
			.setSpeed(new Vector2(rand.nextFloat() * 2 - 1, rand.nextFloat() * 2 - 1));
		
		color = new Color();
	}
	
	public void setColor(Color color) {
		this.color.set(color);
	}
	
	@Override
	public void update(float delta) {
		point1.bounceInside(area);
		point2.bounceInside(area);
		
		point1.move(delta * 20);
		point2.move(delta * 20);
	}
	
	@Override
	public void draw() {
		renderer.setColor(color);
		renderer.begin(ShapeType.Line);
		renderer.line(point1.getLocation(), point2.getLocation());
		renderer.end();
	}
}
